package student.action;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.Map;

import javax.servlet.RequestDispatcher;

import com.util.MyLog;
import com.util.AU1;
//import com.util.Validation;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.util.DB;

public class AdminRegisterUser extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private String commandButton = "";
	private String rollNor="";
	private String username="";
	private String password="";
	private String dob="";
	private String emailId="";
	/*private File image;*/
	private String image;
	private String msg="";
	private String errEmailid = "";
	public String getEmailId() {
		return emailId;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public void setEmailId(String emailId) {
		this.emailId = emailId;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getErrEmailid() {
		return errEmailid;
	}

	public void setErrEmailid(String errEmailid) {
		this.errEmailid = errEmailid;
	}

	public String getFile() {
		return file;
	}

	public void setFile(String file) {
		this.file = file;
	}
	private String file="";
	public String getCommandButton() {
		return commandButton;
	}
	
	public void setCommandButton(String commandButton) {
		this.commandButton = commandButton;
	}
	
	public String getRollNor() {
		return rollNor;
	}
	
	public void setRollNor(String rollNor) {
		this.rollNor = rollNor;
	}
	
	public String getUsername() {
		return username;
	}
	
	public void setUsername(String username) {
		this.username = username;
	}
	
	public String getPassword() {
		return password;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	
	public String getDob() {
		return dob;
	}
	
	public void setDob(String dob) {
		this.dob = dob;
	}
	
	public String getEmailid() {
		return emailId;
	}
	
	public void setEmailid(String emailid) {
		this.emailId = emailid;
	}
	
	/*public File getImage() {
		return image;
	}
	
	public void setImage(File image) {
		this.image = image;
	}*/
	
	public String execute() {
		System.out.println("inside execute query"+rollNor+","+username+","+password+","+dob+","+emailId+","+image);
		
		// if (this.commandButton.startsWith("Register")) {
		
		
	      	int result=insert(rollNor,username,password,dob,emailId,image);
       		 if(result>0){
       			 msg="user"+username +" registered successfully ";
       			 MyLog.log("registered user "+username +" successfully");
       		 }
       		 else{
       			msg="Cannot register user "+username + "";
       			 MyLog.log("Cannot register user "+username +"");
       			 return "error";
       		 }
		// }
  	 	
  	 return "registerSuccess";
        		     
		
	   }		
	public  int insert( String rollNor,String username,String password,String dob,String emailId,String image) {
		System.out.println("inside insert query");
		ResultSet resultSet = null;
		/*String insertquery ="insert into user (roll_No,user_name,password,dob,email_Id,image) values (?,?,?,?,?,?);";*/
	String insertquery ="insert into user (roll_No,user_name,password,dob,email_Id,image) values ('" +rollNor+"','"+username+"','"+password+"','"+dob+"','"+emailId+"','"+image+"')";
	System.out.println("insert query"+insertquery);
	return DB.update(insertquery);
		/*FileInputStream fis=null;
		try{
		Connection conn=DB.getConnection();
		try{
		PreparedStatement ps =conn.prepareStatement(insertquery);
		fis = new FileInputStream(image);
		ps.setString(1, this.rollNor);
		ps.setString(2, this.username);
		ps.setString(3, this.password);
		ps.setString(4, this.dob);
		ps.setString(5, this.emailId);
		ps.setString(6, this.image);
		ps.setBinaryStream(6, fis, (int) image.length());
		ps.executeUpdate();
		}finally{
		fis.close();
		}
	}
		catch(Exception e){
			System.out.println(e);

	    }*/
		// return DB.update(insertquery);
	}
}
	
	